package com.contactmanager;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class AddContact extends Activity {

	Contact contact = new Contact();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Restore preferences
		SharedPreferences settings = getSharedPreferences("preferences", 0);
		int theme = settings.getInt("curTheme", R.style.AppBaseTheme);
		System.out.println(theme);
		setTheme(theme);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_contact);
		// Show the Up button in the action bar.
//		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		Spinner spinner = (Spinner)findViewById(R.id.groupSpinner2);
		spinner.setSelection(contact.getGroup(),true);
		
		new GetAllGroupsTask().execute();
	}

	public void addClicked(View v){
		TextView txtName = (TextView) findViewById(R.id.editName);
		String name = txtName.getText().toString();
		
		TextView txtAddress = (TextView) findViewById(R.id.editAddress);
		String address = txtAddress.getText().toString();
		
		TextView txtCell = (TextView) findViewById(R.id.editGroup1);
		String cell = txtCell.getText().toString();
		
		TextView txtAlternate = (TextView) findViewById(R.id.editAlternate);
		String alt = txtAlternate.getText().toString();
		
		TextView txtEmail = (TextView) findViewById(R.id.editEmail);
		String email = txtEmail.getText().toString();
		
		TextView txtComments = (TextView) findViewById(R.id.editGroup2);
		String comments = txtComments.getText().toString();
		
		Spinner spinner = (Spinner)findViewById(R.id.groupSpinner2);
		int group = spinner.getSelectedItemPosition();
		
		Contact newContact = new Contact();
		newContact.setName(name);
		newContact.setAddress(address);
		newContact.setCell(cell);
		newContact.setAlt(alt);
		newContact.setEmail(email);
		newContact.setComments(comments);
		newContact.setGroup(group);
		
		new AddContactTask().execute(newContact);
		
		Intent myIntent = new Intent(this, AllContacts.class); 
		startActivity(myIntent);
		
	}
	
	private class AddContactTask extends AsyncTask<Contact, Void, Void>{

		@Override
		protected Void doInBackground(Contact... contacts) {
			
			ContactsDataSource cds = new ContactsDataSource(AddContact.this);
			
			cds.open();
			
			for(Contact c : contacts){
				cds.addContact(c);
			}
			updateTime();
			cds.close();
			return null;
		}
		
	}
	
	private class GetAllGroupsTask extends AsyncTask<Void, Integer, ArrayList<Group>>{

		@Override
		protected ArrayList<Group> doInBackground(Void... nothing) {
			
			ContactsDataSource cds3 = new ContactsDataSource(AddContact.this);
			
			cds3.open();
			
			ArrayList<Group> groups = cds3.getAllGroups();
			
			cds3.close();
			
			return groups;
		}
		
        @Override
        protected void onPostExecute(ArrayList<Group> groupList) {
    		Spinner spinner = (Spinner) findViewById(R.id.groupSpinner2);
    		ArrayAdapter<Group> adapter =  new ArrayAdapter<Group>(AddContact.this, android.R.layout.simple_spinner_dropdown_item, groupList);
    		spinner.setAdapter(adapter);        
        }
		
	}
	public void updateTime() {
		SharedPreferences settings = getSharedPreferences("preferences", 0);
		SharedPreferences.Editor editor = settings.edit();
		Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long sysTime = -1;
		String myDate = new String(sdf.format(cal.getTime()));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = format.parse(myDate);
			sysTime=date.getTime();
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		editor.putLong("lastUpdate",sysTime);
		// Commit the edits!
		editor.commit();
	}
}
